/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>

#include <CSLP/CSLP.h>

#include "Dames_constantes.h"
#include "Dames_utilitaires.h"
#include "Dames_main.h"
#include "../types/types.h"

#include "../fonctions.h"
#include "../utilitaires.h"

void MenuDames() {
	bool continuer = true;
	int retourMenu;
	while(continuer) {
		CSLP_ClearScreen();
		retourMenu = AfficherMenuJeu("Jouer\nR\x8Agles\nRetour au menu principal\n", DAMES_TITRE);

		switch(retourMenu) {
			case 1:
				Dame();
				CSLP_ClearScreen();
				continuer = AfficherMenuRecommencer(DAMES_TITRE);
				break;
			case 2:
				printf("Pas encore disponible...\n");
				CSLP_Sleep(500);
				break;
			case 3:
				continuer = false;
				break;
		}

	}
}

void Dame() {
	bool continuer = true;
	bool whitePlayerTurn = true, peutAnnulerSelect = false;
	int nbPionBlanc = DAMES_PION_MAX;
	int nbPionNoir = DAMES_PION_MAX;
	DAMES_TURNSTEP step = SELECT;
	DAMES_CASEVALUE tableauDeJeu[DAMES_ARRAY_SIZE][DAMES_ARRAY_SIZE];
	DAMES_POS pos;
	char player2[maxPlayerNameLenght];
	int touche, nbTour = 1;

	//initialisation du tableau de jeu
	Dames_InitArray(tableauDeJeu);
	//initialisation des positions
	pos.curseur.x = DAMES_ARRAY_SIZE / 2;
	pos.curseur.y = pos.curseur.x;
	Dames_Ajouter(tableauDeJeu, CURSOR, pos.curseur);
	pos.select.x = -1;
	pos.select.y = -1;
	pos.dest = NULL;
	pos.destLen = 0;
	//initialisation du nom des joueurs
	GetPlayerName(1, prenom);
	GetPlayerName(2, player2);

	//boucle principale
	do {
		//affichage du jeu (tableau plus consignes)
		CSLP_ClearScreen();
		if(whitePlayerTurn)
			Dames_Display(tableauDeJeu, prenom, step, nbTour);
		else
			Dames_Display(tableauDeJeu, player2, step, nbTour);
		//traitement des entrées
		touche = CSLP_WaitChar();
		if(touche == '\n' || touche == KEY_BACKSPACE || touche == ' ') {
			switch(step) {
				case SELECT:
					if(touche != KEY_BACKSPACE) {
						//peutAnnulerSelect = false;
						if(Dames_SelectValid(tableauDeJeu, pos.curseur, whitePlayerTurn)) {
							pos.select = pos.curseur;
							step = DESTINATION;
						}
						else
							step = ERROR_SELECT;
					}/*
					else
					{

						if (peutAnnulerSelect)
						{
							Dames_restore(tableauDeJeu, &pos);
							nbTour++;
						}
					}*/
					break;
				case DESTINATION:
					if(touche == '\n') {
						if(pos.destLen) {
							step = VALID;
						}
					}
					else if(touche ==  ' ') {
						if(Dames_DestValid(tableauDeJeu, &pos, whitePlayerTurn)) {
							Dames_AddDest(tableauDeJeu, &pos);
						}
						else {
							step = ERROR_DEST;
						}
					}
					else if(touche == KEY_BACKSPACE) {
						step = SELECT;
						Dames_ClearDest(tableauDeJeu, &pos);
					}
					break;
				case VALID:
					if(touche == '\n') {
						//peutAnnulerSelect = true;
						ChangeBool(&whitePlayerTurn);
						if(whitePlayerTurn)
							Dames_Move(tableauDeJeu, pos, &nbPionNoir);
						else
							Dames_Move(tableauDeJeu, pos, &nbPionBlanc);
						Dames_ClearDest(tableauDeJeu, &pos);
					}
					else if(touche == KEY_BACKSPACE) {
						step = DESTINATION;
					}
					break;
			}
		}
		else if(touche == toucheHaut)
			Dames_MoveCursor(tableauDeJeu, &pos.curseur, HAUT);
		else if(touche == toucheBas)
			Dames_MoveCursor(tableauDeJeu, &pos.curseur, BAS);
		else if(touche == toucheDroite)
			Dames_MoveCursor(tableauDeJeu, &pos.curseur, DROITE);
		else if(touche == toucheGauche)
			Dames_MoveCursor(tableauDeJeu, &pos.curseur, GAUCHE);
		else if(touche ==  KEY_ESCAPE) {
			int choix;
			CSLP_ClearScreen();
			choix = AfficherMenu("Oui\nNon\n", "Etes-vous sur de vouloir quitter?", DAMES_TITRE);
			if(choix == 1)
				continuer = false;
		}

	}
	while(continuer);
	return;
}
